<?php
/**
 * User: FDG348
 * Date: 2018/3/8
 * Time: 16:05
 *
 * 修改历史
 * 修改时间：2018/3/8
 * 修改人：lizenan
 * 修改功能：【后台需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页。
 * 任务ID：533
 */
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\BrandStreetCategory;
use common\helps\tools;
use backend\models\BannerConfig;

class BrandCategoryController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'BrandCategory';
    
    public function actionIndex()
    {
        return $this->render('index');
    }
    
    public function actionGetBrandCategory(){
        $data = Yii::$app->request->post();
        $brand_category = BrandStreetCategory::find();
        $count = $brand_category->count();
        $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
        $list = $brand_category->offset($data['start'])->limit($data['length'])->orderBy(
            [$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
        if (is_array($list) && count($list) > 0)foreach ($list as $key=>&$list_val){
            $list_val['num'] = $key+1;
        }
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeBrandCategoryStatus(){
        $model = new BrandStreetCategory();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        if (!$use_flg){
            $banner_config = BannerConfig::find()->where('brand_street_category_id='.$id)->asArray()->all();
            if (is_array($banner_config) && count($banner_config) > 0)foreach ($banner_config as $banner_config_val){
                $banner = BannerConfig::find()->where('group_id='.$banner_config_val['group_id'])->asArray()->all();
                if (count($banner) > 1){
                    BannerConfig::deleteAll('id='.$banner_config_val['id']);
                }else{
                    BannerConfig::updateAll(['brand_street_category_id'=>null,'editor'=>$model->getCreator(),'edit_time'=>date('Y-m-d H:i:s')],'id='.$banner[0]['id']);
                }
            }
        }
        $data_new = [
            'use_flg'=>$use_flg,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            throw new \Exception('操作失败');
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionSaveBrandCategory(){
        $brand_catehory = new BrandStreetCategory();
        $data = Yii::$app->request->post();
        $name = trim($data['name']);
        $sort = $data['sort'] ? $data['sort'] : 0;
        $creator = $brand_catehory->getCreator();
        $oper_time = date('Y-m-d H:i:s');
        if (!$name){
            $this->retJson('请填写品牌街品牌分类名称',500);
        }
        if (intval($data['id']) > 0){
            $brand_catehory = BrandStreetCategory::findOne($data['id']);
            if (!tools::arrayHasValue($brand_catehory,'id')){
                $this->retJson('品牌街品牌分类不存在',500);
            }
            $brand_catehory->editor = $creator;
            $brand_catehory->edit_time = $oper_time;
        }else{
            $brand_catehory->use_flg = $brand_catehory::STATUS_ENABLE;
            $brand_catehory->creator = $creator;
            $brand_catehory->create_time = $oper_time;
        }
        $brand_catehory->name = $name;
        $brand_catehory->sort = $sort;
        $brand_catehory->save();
        $brand_catehory_id = $brand_catehory->attributes['id'];
        if (!$brand_catehory_id){
            $this->retJson('操作失败，请重试',500);
        }
        $this->retJson('操作成功',200);
    }
}